<template>
  <div>
    <!-- 三级分类组件 -->
    <Category :scene="scene"></Category>
    <el-card style="margin: 10px 0px">
      <!-- v-if|v-show不用销毁和创建 -->
      <div v-show="scene == 0">
        <el-button
          @click="addSpu"
          type="primary"
          size="default"
          icon="Plus"
          :disabled="categoryStore.c3Id ? false : true"
        >
          添加SPU
        </el-button>
        <!-- 展示已有SPU数据 -->
        <el-table style="margin: 10px 0px" border :data="records">
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="80px"
          ></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column
            label="SPU描述"
            prop="description"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column label="SPU操作">
            <!-- row已有的SPU -->
            <template #="{ row, $index }">
              <el-button
                type="primary"
                size="small"
                icon="Plus"
                title="添加SKU"
              ></el-button>
              <el-button
                type="primary"
                size="small"
                icon="Edit"
                title="修改SPU"
                @click="updateSpu(row)"
              ></el-button>
              <el-button
                type="primary"
                size="small"
                icon="View"
                title="查看SKU列表"
              ></el-button>
              <el-button
                type="primary"
                size="small"
                icon="Delete"
                title="删除SPU"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[3, 5, 7, 9]"
          :background="true"
          layout="prev, pager, next, jumper, ->, sizes,total"
          :total="total"
          @size-change="changeSize"
          @current-change="getHasSpu"
        />
      </div>
      <!-- 添加SPU和修改SPU -->
      <SpuForm
        ref="spu"
        v-show="scene == 1"
        @changeScene="changeScene"
      ></SpuForm>
      <!-- 添加SKU -->
      <SkuForm v-show="scene == 2"></SkuForm>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'

import { reqHasSpu } from '@/api/product/spu'

import type { HasSpuResponseData, Records } from '@/api/product/spu/type'

//子组件
import SpuForm from './spuForm.vue'
import SkuForm from './skuForm.vue'

//分类仓库
import useCategoryStore from '@/store/modules/category'
import type { SpuData } from '@/api/product/spu/type'
let categoryStore = useCategoryStore()

//场景数据
//0 显示已有SPU
//1 添加或修改有SPU
//2 添加SKU
let scene = ref<number>(0)

//分页器默认页码
let pageNo = ref<number>(1)
//每一页展示几条数据
let pageSize = ref<number>(3)

//存储已有SPU数组
let records = ref<Records>([])

//存储已有SPU总数
let total = ref<number>(0)

//获取子组件实例SpuForm
let spu = ref<any>()

//监听三级分类ID变化
watch(
  () => categoryStore.c3Id,
  async () => {
    if (!categoryStore.c3Id) {
      return
    }
    getHasSpu()
  },
)

//获取某个三级分类下的所有已有SPU
const getHasSpu = async (pager = 1) => {
  //默认第一页
  pageNo.value = pager
  let result: HasSpuResponseData = await reqHasSpu(
    pageNo.value,
    pageSize.value,
    categoryStore.c3Id,
  )
  console.log(result)
  if (result.code == 200) {
    records.value = result.data.records
    total.value = result.data.total
  }
}

//分页器下拉菜单发生变化触发
const changeSize = () => {
  getHasSpu()
}

//添加新的SPU按钮回调
const addSpu = () => {
  //切换为场景1：添加或修改已有SPU
  scene.value = 1
}

//修改已有SPU
const updateSpu = (row: SpuData) => {
  scene.value = 1
  console.log('updateSpu', spu.value)
  //调用子组件的方法获取已有SPU
  spu.value.initHasSpuData(row)
}

//子组件SpuForm绑定的自定义事件：子组件通知父逐渐切换场景为0
const changeScene = (num: number) => {
  //子组件点击取消
  scene.value = num
}
</script>

<style></style>
